home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 23 web forms and controls / databinding / datalistform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-17  |  9.4 KB  |  222 lines

  1. Imports System.Data.OleDb
  2.  
  3. Public Class DataListForm
  4.     Inherits System.Web.UI.Page
  5.  
  6.     Protected WithEvents dlstBooks As System.Web.UI.WebControls.DataList
  7.     Protected WithEvents btnDisplay As System.Web.UI.WebControls.Button
  8.     Protected WithEvents txtPath As System.Web.UI.WebControls.TextBox
  9.     Protected WithEvents btnDisplayFileNames As System.Web.UI.WebControls.Button
  10.     Protected WithEvents rptFilenames As System.Web.UI.WebControls.Repeater
  11.     Protected WithEvents dlstFiles As System.Web.UI.WebControls.DataList
  12.  
  13. #Region " Web Form Designer Generated Code "
  14.  
  15.     'This call is required by the Web Form Designer.
  16.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  17.  
  18.     End Sub
  19.  
  20.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  21.         'CODEGEN: This method call is required by the Web Form Designer
  22.         'Do not modify it using the code editor.
  23.         InitializeComponent()
  24.     End Sub
  25.  
  26. #End Region
  27.  
  28.     ' display the list of book the first time the page is loaded
  29.  
  30.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  31.         If Not Me.IsPostBack Then
  32.             BindBookList()
  33.         End If
  34.     End Sub
  35.  
  36.     ' close the connection when the page unloads
  37.  
  38.     Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
  39.         ' Close the connection if still open
  40.         If cn.State = ConnectionState.Open Then cn.Close()
  41.     End Sub
  42.  
  43.     ' The connection to the Pubs database.
  44.     Dim cn As New OleDbConnection(OledbPubsConnString)
  45.  
  46.     Sub BindBookList()
  47.         ' Open a connection to Pubs, if necessary.
  48.         If cn.State = ConnectionState.Closed Then cn.Open()
  49.  
  50.         ' Read title name and key from the Titles table.
  51.         Dim sql As String = "SELECT title_id, title FROM Titles"
  52.         ' If we are in select or edit mode, we must read more data.
  53.         If dlstBooks.SelectedIndex >= 0 Or dlstBooks.EditItemIndex >= 0 Then
  54.             sql = "SELECT Titles.*, Publishers.pub_name FROM Titles " _
  55.                 & "INNER JOIN Publishers ON Titles.pub_id=Publishers.pub_id"
  56.         End If
  57.  
  58.         Dim cmd As New OleDbCommand(sql, cn)
  59.         Dim dr As OleDbDataReader = cmd.ExecuteReader
  60.         ' Bind the DataReader to the DataList control.
  61.         dlstBooks.DataSource = dr
  62.         dlstBooks.DataKeyField = "title_id"
  63.         dlstBooks.DataBind()
  64.         dr.Close()
  65.  
  66.         ' if in edit mode, we must also fill the ddlPublisher DropDownList control
  67.         If dlstBooks.EditItemIndex >= 0 Then
  68.             ' Get a reference to the DataListItem.
  69.             Dim dli As DataListItem = dlstBooks.Items(dlstBooks.EditItemIndex)
  70.             ' Get a reference to the ddlPublishers DropDownList control.
  71.             Dim ddlPublishers As DropDownList = DirectCast(dli.FindControl("ddlPublishers"), DropDownList)
  72.             ' Get a reference to the PubId hidden Literal control
  73.             Dim litPubId As Literal = DirectCast(dli.FindControl("litPubId"), Literal)
  74.  
  75.             ' Fill the list of publishers.
  76.             cmd = New OleDbCommand("SELECT pub_id, pub_name FROM Publishers", cn)
  77.             dr = cmd.ExecuteReader
  78.             ddlPublishers.DataSource = dr
  79.             ddlPublishers.DataTextField = "pub_name"
  80.             ddlPublishers.DataValueField = "pub_id"
  81.             ddlPublishers.DataBind()
  82.             dr.Close()
  83.  
  84.             ' Highlight the element corresponding to the title's publisher.
  85.             SelectItemFromValue(ddlPublishers, litPubId.Text)
  86.         End If
  87.     End Sub
  88.  
  89.     ' this event handler fires when a new DataList row is selected
  90.  
  91.     Private Sub dlstBooks_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dlstBooks.SelectedIndexChanged
  92.         ' End the edit operation, if any.
  93.         dlstBooks.EditItemIndex = -1
  94.         BindBookList()
  95.     End Sub
  96.  
  97.     ' this event handler fires when the Edit button is clicked
  98.  
  99.     Private Sub dlstBooks_EditCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlstBooks.EditCommand
  100.         ' Hide any selected item, if any, and select the current item
  101.         dlstBooks.SelectedIndex = -1
  102.         dlstBooks.EditItemIndex = e.Item.ItemIndex
  103.         BindBookList()
  104.     End Sub
  105.  
  106.     ' this event handler fires when the Cancel button is clicked
  107.  
  108.     Private Sub dlstBooks_CancelCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlstBooks.CancelCommand
  109.         ' End any edit operation.
  110.         dlstBooks.EditItemIndex = -1
  111.         BindBookList()
  112.     End Sub
  113.  
  114.     ' this event handler fires when the Delete button is clicked
  115.  
  116.     Private Sub dlstBooks_DeleteCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlstBooks.DeleteCommand
  117.         ' Retrieve the ID of the record to be deleted.
  118.         Dim id As String = dlstBooks.DataKeys(e.Item.ItemIndex).ToString
  119.         ' Open the connection if necessary.
  120.         If cn.State = ConnectionState.Closed Then cn.Open()
  121.         ' Delete this row.
  122.         Dim cmd As New OleDbCommand("DELETE Titles WHERE title_id='" & id & "'", cn)
  123.         'cmd.ExecuteNonQuery()
  124.         ' Rebind the control.
  125.         BindBookList()
  126.     End Sub
  127.  
  128.     ' this event handler fires when the Update button is clicked
  129.  
  130.     Private Sub dlstBooks_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlstBooks.UpdateCommand
  131.         ' Get the ID of the record to be deleted.
  132.         Dim title_id As String = dlstBooks.DataKeys(e.Item.ItemIndex).ToString
  133.  
  134.         ' This is the DataListItem being edited.
  135.         Dim dli As DataListItem = dlstBooks.Items(e.Item.ItemIndex)
  136.  
  137.         ' Get the values of the txtTitle child control.
  138.         Dim tb As TextBox = DirectCast(dli.FindControl("txtTitle"), TextBox)
  139.         Dim title As String = tb.Text
  140.         ' Do the same with the txtType and txtPrice controls, but use a shorter syntax.
  141.         Dim type As String = DirectCast(dli.FindControl("txtType"), TextBox).Text
  142.         Dim price As Decimal = CDec(DirectCast(dli.FindControl("txtPrice"), TextBox).Text)
  143.         ' Get the value of the selected element in ddlPublishers 
  144.         Dim ddlPublishers As DropDownList = DirectCast(dli.FindControl("ddlPublishers"), DropDownList)
  145.         Dim pub_id As String = ddlPublishers.SelectedItem.Value
  146.  
  147.         ' Prepare to update this record.
  148.         Dim cmd As New OleDbCommand("UPDATE Titles SET title=?, pub_id=?, type=?, price=? WHERE title_id=?", cn)
  149.         cmd.Parameters.Add("@title", title)
  150.         cmd.Parameters.Add("@pub_id", pub_id)
  151.         cmd.Parameters.Add("@type", type)
  152.         cmd.Parameters.Add("@price", price)
  153.         cmd.Parameters.Add("@title_id", title_id)
  154.  
  155.         ' Open the connection if necessary, and execute the command
  156.         If cn.State = ConnectionState.Closed Then cn.Open()
  157.         cmd.ExecuteNonQuery()
  158.  
  159.         ' End the edit mode and rebind the control.
  160.         dlstBooks.EditItemIndex = -1
  161.         dlstBooks.SelectedIndex = e.Item.ItemIndex
  162.         BindBookList()
  163.     End Sub
  164.  
  165.     '------------------------------------------------------------
  166.     ' the following procedures are related to the 2nd
  167.     ' DataList control on the page
  168.     '------------------------------------------------------------
  169.  
  170.     Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
  171.         ' This is the directory we want to browse.
  172.         Dim path As String = txtPath.Text
  173.         ' This is the ArrayList that works as the data source.
  174.         Dim arrFiles As New ArrayList(100)
  175.  
  176.         Dim file As String
  177.         For Each file In System.IO.Directory.GetFiles(path)
  178.             ' Filter files on their extension.
  179.             Select Case System.IO.Path.GetExtension(file).ToLower
  180.                 Case ".gif", ".ico", ".bmp", ".jpg", ".jpeg"
  181.                     arrFiles.Add(file)
  182.             End Select
  183.         Next
  184.  
  185.         ' Bind the ArrayList to the DataList
  186.         dlstFiles.DataSource = arrFiles
  187.         dlstFiles.DataBind()
  188.     End Sub
  189.  
  190.     ' Return the list of selected files.
  191.  
  192.     Function GetSelectedFiles() As ArrayList
  193.         Dim arr As New ArrayList(100)
  194.  
  195.         Dim dli As DataListItem
  196.         For Each dli In dlstFiles.Items
  197.             ' Get a reference to the CheckBox control.
  198.             Dim chkFile As CheckBox = DirectCast(dli.FindControl("chkFile"), CheckBox)
  199.             If chkFile.Checked Then
  200.                 ' If the checkbox has been checked, get a reference to the Image control.
  201.                 With DirectCast(dli.FindControl("imgFile"), WebControls.Image)
  202.                     ' Add the file name to the list of result.
  203.                     arr.Add(.ImageUrl)
  204.                 End With
  205.             End If
  206.         Next
  207.         ' Return the result array.
  208.         Return arr
  209.     End Function
  210.  
  211.     ' display the list of selected image files
  212.  
  213.     Private Sub btnDisplayFileNames_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayFileNames.Click
  214.         ' get the list of selected files
  215.         Dim arr As ArrayList = GetSelectedFiles()
  216.         ' display them in the Repeater control
  217.         rptFilenames.DataSource = arr
  218.         rptFilenames.DataBind()
  219.     End Sub
  220. End Class
  221.  
  222.